using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using ProductService.Domain.Repositories;
using ProductService.Infrastructure.Repositories;

namespace ProductService.Infrastructure
{
    /// <summary>
    /// 基础设施层依赖注入配置
    /// </summary>
    public static class DependencyInjection
    {
        /// <summary>
        /// 添加基础设施服务
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="configuration">配置</param>
        /// <returns>服务集合</returns>
        public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
        {
            // 注册SqlSugarClient
            services.AddSingleton<ISqlSugarClient>(s =>
            {
                var connectionString = configuration.GetConnectionString("ProductServiceDB");
                var db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = connectionString,
                    DbType = DbType.SqlServer,
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.Attribute
                });
                return db;
            });

            // 注册仓储
            services.AddScoped<IProductRepository, ProductRepository>();

            return services;
        }
    }
}